package cn.eleven.gui;


import cn.eleven.entity.User;
import cn.eleven.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;

/**
 * 用户注册窗口
 */
public class RegisterDialogUI extends JDialog {
    public RegisterDialogUI(JFrame parent, UserService userService) {
        super(parent, "用户注册", true);
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.anchor = GridBagConstraints.WEST;

        // 标题
        JLabel titleLabel = new JLabel("注册新用户");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        gbc.gridwidth = 2;
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(titleLabel, gbc);

        // 用户名输入
        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 1;
        add(new JLabel("用户名:"), gbc);

        JTextField usernameField = new JTextField(20);
        gbc.gridx = 1;
        add(usernameField, gbc);

        // 密码输入
        gbc.gridx = 0;
        gbc.gridy = 2;
        add(new JLabel("密码:"), gbc);

        JPasswordField passwordField = new JPasswordField(20);
        gbc.gridx = 1;
        add(passwordField, gbc);

        // 确认密码输入
        gbc.gridx = 0;
        gbc.gridy = 3;
        add(new JLabel("确认密码:"), gbc);

        JPasswordField confirmPasswordField = new JPasswordField(20);
        gbc.gridx = 1;
        add(confirmPasswordField, gbc);

        // 注册按钮
        JButton registerButton = new JButton("注册");
        registerButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
        registerButton.setBackground(new Color(0, 150, 136));
        registerButton.setForeground(Color.BLACK);
        registerButton.setFocusPainted(false);
        registerButton.addActionListener(e -> {
            String username = usernameField.getText().trim();
            String password = new String(passwordField.getPassword());
            String confirmPassword = new String(confirmPasswordField.getPassword());

            if (username.isEmpty() || password.isEmpty()) {
                JOptionPane.showMessageDialog(this, "用户名和密码不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (!password.equals(confirmPassword)) {
                JOptionPane.showMessageDialog(this, "两次密码输入不一致！", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            try {
                if (userService.isUsernameTaken(username)) {
                    JOptionPane.showMessageDialog(this, "用户名已存在，请换一个！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                userService.registerUser(new User(username, password));
                JOptionPane.showMessageDialog(this, "注册成功！请登录。", "成功", JOptionPane.INFORMATION_MESSAGE);
                dispose(); // 关闭注册窗口
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "注册失败，请稍后重试！", "错误", JOptionPane.ERROR_MESSAGE);
                ex.printStackTrace();
            }
        });

        gbc.gridwidth = 2;
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.CENTER;
        add(registerButton, gbc);

        // 设置窗口属性
        pack();
        setLocationRelativeTo(parent);
        setVisible(true);
    }
}
